home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / DTS.Lib & Samples / Kibitz / AppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-22  |  8.7 KB  |  342 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        appleevents.c
  5. ** Written by:  Keith Rollin
  6. **
  7. ** Copyright © 1990-1992 Apple Computer, Inc.
  8. ** All rights reserved.
  9. **
  10. ** This code is completely based on the great work done by Keith Rollin.
  11. ** All I did was to add more comments than _anybody_ would want, make some
  12. ** things a little more general, and to set the code up so the the custom
  13. ** events are handled in a separate file. */
  14.  
  15.  
  16.  
  17. /*****************************************************************************/
  18.  
  19.  
  20.  
  21. #include "Kibitz.h"                /* Get the Kibitz includes/typedefs, etc.    */
  22. #include "KibitzCommon.h"        /* Get the stuff in common with rez.        */
  23. #include "Kibitz.protos"        /* Get the prototypes for Kibitz.            */
  24.  
  25. #ifndef __AEUTILS__
  26. #include <AEUtils.h>
  27. #endif
  28.  
  29. #ifndef __GESTALTEQU__
  30. #include <GestaltEqu.h>
  31. #endif
  32.  
  33. #ifndef __UTILITIES__
  34. #include <Utilities.h>
  35. #endif
  36.  
  37.  
  38.  
  39. /*****************************************************************************/
  40.  
  41.  
  42.  
  43. #define rErrorAlert 129
  44. #define kTimeOutInTicks (60 * 30)    /* 30 second timeout. */
  45.  
  46.  
  47.  
  48. /*****************************************************************************/
  49.  
  50.  
  51.  
  52. struct triplets{
  53.     AEEventClass    theEventClass;
  54.     AEEventID        theEventID;
  55.     ProcPtr            theHandler;
  56. };
  57. typedef struct triplets triplets;
  58. static triplets keywordsToInstall[] = {
  59.     { kCoreEventClass,        kAEOpenApplication,        KibitzAEOpenApplication },
  60.     { kCoreEventClass,        kAEOpenDocuments,        KibitzAEOpenDocuments },
  61.     { kCoreEventClass,        kAEPrintDocuments,        KibitzAEPrintDocuments },
  62.     { kCoreEventClass,        kAEQuitApplication,        KibitzAEQuitApplication }
  63.         /* The above are the four required AppleEvents. */
  64. };
  65.  
  66. Boolean        gHasAppleEvents = false;
  67. Boolean        gHasPPCToolbox  = false;
  68.  
  69.  
  70.  
  71. /*****************************************************************************/
  72.  
  73.  
  74.  
  75. extern Boolean    gQuitApplication;
  76. extern Cursor    *gCurrentCursor;
  77. extern short    gPrintPage;
  78.  
  79.  
  80.  
  81. /*****************************************************************************/
  82. /*****************************************************************************/
  83.  
  84.  
  85.  
  86. /* InitAppleEvents
  87. **
  88. ** Intialize our AppleEvent dispatcher table.  For every triplet of entries in
  89. ** keywordsToInstall, we make a call to AEInstallEventHandler(). */
  90.  
  91. #pragma segment AppleEvents
  92. void    InitAppleEvents(void)
  93. {
  94.     OSErr    err;
  95.     long    result;
  96.     short    i;
  97.  
  98.     gHasPPCToolbox  = (Gestalt(gestaltPPCToolboxAttr, &result) ? false : result != 0);
  99.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? false : result != 0);
  100.  
  101.     if (gHasAppleEvents) {
  102.         for (i = 0; i < (sizeof(keywordsToInstall) / sizeof(triplets)); ++i) {
  103.             err = AEInstallEventHandler(
  104.                 keywordsToInstall[i].theEventClass,    /* What class to install.  */
  105.                 keywordsToInstall[i].theEventID,    /* Keywords to install.    */
  106.                 keywordsToInstall[i].theHandler,    /* The AppleEvent handler. */
  107.                 0L,                                    /* Unused refcon.           */
  108.                 false                                /* Only for our app.       */
  109.             );
  110.  
  111.             if (err) {
  112.                 Alert(rErrorAlert, (ModalFilterProcPtr)AlertFilter);
  113.                 return;
  114.             }
  115.         }
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. /*****************************************************************************/
  122. /*****************************************************************************/
  123.  
  124.  
  125.  
  126. #pragma segment AppleEvents
  127. pascal OSErr    KibitzAEOpenApplication(AppleEvent message, AppleEvent reply, long refcon)
  128. {
  129. #pragma unused (message, refcon)
  130.  
  131.     FileRecHndl    frHndl;
  132.     OSErr        err;
  133.  
  134.     gCurrentCursor = nil;
  135.         /* Force re-calc of cursor region and cursor to use. */
  136.  
  137.     err = AppNewDocument(&frHndl, ksOrigName);
  138.     if (!err)
  139.         (*frHndl)->doc.compMovesBlack = true;
  140.         if (err = AppNewWindow(frHndl, nil, (WindowPtr)-1))
  141.             AppDisposeDocument(frHndl);
  142.  
  143.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  144.         &reply,                    /* The AppleEvent.              */
  145.         keyReplyErr,            /* AEKeyword                 */
  146.         typeShortInteger,        /* Desired type.             */
  147.         (Ptr)&err,                /* Pointer to area for data. */ 
  148.         sizeof(short)            /* Size of data area.         */
  149.     );
  150.  
  151.     return(err);
  152. }
  153.  
  154.  
  155.  
  156. /*****************************************************************************/
  157.  
  158.  
  159.  
  160. #pragma segment AppleEvents
  161. pascal OSErr    KibitzAEOpenDocuments(AppleEvent message, AppleEvent reply, long refcon)
  162. {
  163. #pragma unused (refcon)
  164.  
  165.     OSErr        err;
  166.  
  167.     gCurrentCursor = nil;
  168.         /* Force re-calc of cursor region and cursor to use. */
  169.  
  170.     err = OpenDocEventHandler(message, reply, 0);
  171.  
  172.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  173.         &reply,                    /* The AppleEvent.              */
  174.         keyReplyErr,            /* AEKeyword                 */
  175.         typeShortInteger,        /* Desired type.             */
  176.         (Ptr)&err,                /* Pointer to area for data. */ 
  177.         sizeof(short)            /* Size of data area.         */
  178.     );
  179.  
  180.     return(err);
  181. }
  182.  
  183.  
  184.  
  185. /*****************************************************************************/
  186.  
  187.  
  188.  
  189. #pragma segment AppleEvents
  190. pascal OSErr    KibitzAEPrintDocuments(AppleEvent message, AppleEvent reply, long refcon)
  191. {
  192. #pragma unused (refcon)
  193.  
  194.     OSErr        err;
  195.     short        openMode;
  196.  
  197.     DoSetCursor(&qd.arrow);
  198.  
  199.     openMode = 1;
  200.     if (!AEInteractWithUser(kTimeOutInTicks, nil, nil)) ++openMode;
  201.  
  202.     err = OpenDocEventHandler(message, reply, openMode);
  203.  
  204.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  205.         &reply,                    /* The AppleEvent.              */
  206.         keyReplyErr,            /* AEKeyword                 */
  207.         typeShortInteger,        /* Desired type.             */
  208.         (Ptr)&err,                /* Pointer to area for data. */ 
  209.         sizeof(short)            /* Size of data area.         */
  210.     );
  211.  
  212.     return(err);
  213. }
  214.  
  215.  
  216.  
  217. /*****************************************************************************/
  218.  
  219.  
  220.  
  221. #pragma segment AppleEvents
  222. pascal OSErr    KibitzAEQuitApplication(AppleEvent message, AppleEvent reply, long refcon)
  223. {
  224. #pragma unused (message, refcon)
  225.  
  226.     OSErr    err;
  227.  
  228.     gCurrentCursor = nil;
  229.         /* Force re-calc of cursor region and cursor to use. */
  230.  
  231.     if (CloseAllWindows()) {
  232.         gQuitApplication = true;
  233.         err = noErr;
  234.     }
  235.     else err = errAEEventNotHandled;
  236.  
  237.     AEPutParamPtr(                /* RETURN REPLY ERROR, EVEN IF NONE... */
  238.         &reply,                    /* The AppleEvent.              */
  239.         keyReplyErr,            /* AEKeyword                 */
  240.         typeShortInteger,        /* Desired type.             */
  241.         (Ptr)&err,                /* Pointer to area for data. */ 
  242.         sizeof(short)            /* Size of data area.         */
  243.     );
  244.  
  245.     return(noErr);
  246. }
  247.  
  248.  
  249.  
  250. /*****************************************************************************/
  251.  
  252.  
  253.  
  254. /* OpenDocEventHandler
  255. **
  256. ** Called when we recieve an AppleEvent with an ID of "kAEOpenDocuments".
  257. ** This routine gets the direct parameter, parses it up into little FSSpecs,
  258. ** and opens each indicated file.  It also shows the technique to be used in
  259. ** determining if you are doing everything the AppleEvent record is telling
  260. ** you.  Parameters can be divided up into two groups: required and optional.
  261. ** Before executing an event, you must make sure that you've read all the
  262. ** required events.  This is done by making an "any more?" call to the
  263. ** AppleEvent manager. */
  264.  
  265. #pragma segment AppleEvents
  266. OSErr    OpenDocEventHandler(AppleEvent message, AppleEvent reply, short mode)
  267. {
  268. #pragma unused (reply)
  269.  
  270.     OSErr        err;
  271.     OSErr        err2;
  272.     AEDesc        theDesc;
  273.     FSSpec        theFSS;
  274.     short        loop;
  275.     long        numFilesToOpen;
  276.     AEKeyword    ignoredKeyWord;
  277.     DescType    ignoredType;
  278.     Size        ignoredSize;
  279.     FileRecHndl    frHndl;
  280.     WindowPtr    docWindow;
  281.  
  282.     theDesc.dataHandle = nil;
  283.         /* Make sure disposing of the descriptors is okay in all cases.
  284.         ** This will not be necessary after 7.0b3, since the calls that
  285.         ** attempt to create the descriptors will nil automatically
  286.         ** upon failure. */
  287.  
  288.     if (err = AEGetParamDesc(&message, keyDirectObject, typeAEList, &theDesc))
  289.         return(err);
  290.  
  291.     if (!MissedAnyParameters(&message)) {
  292.  
  293. /* Got all the parameters we need.  Now, go through the direct object,
  294. ** see what type it is, and parse it up. */
  295.  
  296.         err = AECountItems(&theDesc, &numFilesToOpen);
  297.         if (!err) {
  298.             /* We have numFilesToOpen that need opening, as either a window
  299.             ** or to be printed.  Go to it... */
  300.  
  301.             for (loop = 1; ((loop <= numFilesToOpen) && (!err)); ++loop) {
  302.                 err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  303.                     &theDesc,            /* List of file names.             */
  304.                     loop,                /* Item # in the list.             */
  305.                     typeFSS,            /* Item is of type FSSpec.         */
  306.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  307.                     &ignoredType,        /* Returned type -- we know.     */
  308.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  309.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  310.                     &ignoredSize        /* Actual size -- we know.         */
  311.                 );
  312.                 if (err) break;
  313.  
  314.                 err = AppOpenDocument(&frHndl, &theFSS, fsRdWrPerm);
  315.                 if (err) break;
  316.  
  317.                 gPrintPage = mode;
  318.                     /* Open the window off-screen if we are printing. */
  319.                 if (err = AppNewWindow(frHndl, &docWindow, (WindowPtr)-1))
  320.                     AppDisposeDocument(frHndl);
  321.                 else {
  322.                     if (gPrintPage) {
  323.                         err  = AppPrintDocument(frHndl, (mode == 2), (loop == 1));
  324.                         mode = 1;
  325.                         AppDisposeDocument(frHndl);
  326.                         DisposeAnyWindow(docWindow);
  327.                     }
  328.                     else AppAutoLaunch(frHndl);
  329.                 }
  330.                 gPrintPage = 0;
  331.             }
  332.         }
  333.     }
  334.     AppPrintDocument(nil, false, false);    /* Clean up after printing, if we did any. */
  335.  
  336.     err2 = AEDisposeDesc(&theDesc);
  337.     return(err ? err : err2);
  338. }
  339.  
  340.  
  341.  
  342.